﻿using System;
using Game.ExtensibleObjects;
using Game.Map;

namespace Game.Command
{
    public static class CommandExtensionHelper
    {
        public static CommandMapExtension CommandX(this IMapField field)
        {
            return field.Extensions.Find<CommandMapExtension>();
        }
    }

    public class CommandEventArgs : EventArgs
    {
        readonly IMapField _MapField;

        public CommandEventArgs(IMapField field)
        {
            _MapField = field;
        }

        public IMapField MapField
        {
            get { return _MapField; }
        }
    }
    
    public class CommandMapExtensionFactory : IRuntimeExtensionFactory
    {
        public IRuntimeExtension CreateExtension(IRuntimeExtensibleObject source)
        {
            return new CommandMapExtension(source as IMapField);
        }
    }

    public class CommandMapExtension : IRuntimeExtension
    {
        public event EventHandler<CommandEventArgs> CommandAssigned;
        
        readonly IMapField _MapField;
        GameCommand _Command;
        
        public CommandMapExtension(IMapField field)
        {
            _MapField = field;
        }
        
        public GameCommand Command 
        { 
            get { return _Command; }
            set { _Command=value; FireChanged(); }
        }
        
        void FireChanged()
        {
            if (CommandAssigned != null)
            {
                CommandAssigned(this, new CommandEventArgs(_MapField));
            }
        }
    }
}
